package com.bussiness.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bussiness.auto.JSONAuthentication;
import com.bussiness.entity.BaApartment;
import com.bussiness.entity.BaFbtask;
import com.bussiness.entity.BaNotice;
import com.bussiness.entity.BaWctask;
import com.bussiness.mapper.BaFbtaskMapper;
import com.bussiness.mapper.BaWctaskMapper;
import com.bussiness.response.Result;
import com.bussiness.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bussiness
 * @since 2022-03-29
 */
@RestController
@RequestMapping("/ba-wctask")
public class BaWctaskController extends JSONAuthentication {
    @Autowired
    BaWctaskMapper baWctaskMapper;
    @Autowired
    BaFbtaskMapper baFbtaskMapper;
    @ApiOperation(value = "上传任务")
    @PostMapping("/add")
    public void addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response,@RequestParam("userId") int userId,@RequestParam("tastId") int tastId) throws  Exception{
        BaWctask baWctask=new BaWctask();
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        baWctask.setUserId(userId);
        baWctask.setTastId(tastId);
        baWctask.setCreatTime(timestamp);
        byte[] bytes = file.getBytes();
        String imageFileName = file.getName();
        Path path = Paths.get("D:\\自己的项目\\公司内部管理系统\\bubussss\\public\\file\\" + imageFileName);
        Files.write(path, bytes);//写入文件
        String avatar_url=imageFileName;
        baWctask.setFile(avatar_url);
        response.reset();
        BaFbtask baFbtask = baFbtaskMapper.selectById(baWctask.getTastId());
        baFbtask.setStatus("已完成");
        if(baWctaskMapper.insert(baWctask)>0&&baFbtaskMapper.updateById(baFbtask)>0){
            Result ok = Result.ok().message("提交成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("提交失败");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除任务文件")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        if(baWctaskMapper.deleteById(id)>0){
            BaWctask baWctask = baWctaskMapper.selectById(id);
            File file=new File("D:\\自己的项目\\公司内部管理系统\\bubussss\\public\\file\\"+baWctask.getFile());
            file.delete();
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有提交的任务文件")
    @PostMapping("/findAllTaskFile")
    public void findAllTaskFile(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<BaWctask> shUserPage=new Page<>(current,size);
        Page<BaWctask> shUserPages=baWctaskMapper.selectPage(shUserPage,null);
        List<BaWctask> records = shUserPages.getRecords();
        int total=baWctaskMapper.selectCount(null);
        for (BaWctask a :records){
            a.setBaFbtask(baFbtaskMapper.selectById(a.getTastId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("notice",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有提交的任务文件通过用户id")
    @PostMapping("/findAllTaskFileByUserId")
    public void findAllTaskFileByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<BaWctask> shUserPage=new Page<>(current,size);
        Page<BaWctask> shUserPages=baWctaskMapper.selectPage(shUserPage,wrapper);
        List<BaWctask> records = shUserPages.getRecords();
        int total=baWctaskMapper.selectCount(wrapper);
        for (BaWctask a :records){
            a.setBaFbtask(baFbtaskMapper.selectById(a.getTastId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("notice",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

